<?php

namespace App\Http\Requests;

use App\Enums\ResponseCode;
use App\Exceptions\BusinessException;
use App\Traits\ApiResponse;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;

abstract class AbstractBaseRequest extends FormRequest
{
    use ApiResponse;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * @throws BusinessException
     */
    protected function failedValidation(Validator $validator)
    {
        $errorMsg = $validator->errors()->first();
        // 将空格和句号替换成空
        if(app()->getLocale() == 'zh_CN'){
            $info = str_replace([' ','。'],'',$errorMsg);
        }else{
            $info = $errorMsg;
        }
        $this->exception(ResponseCode::VALIDATION_ERROR,$info);
    }
}
